package jpa;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;


	/**
	 * JPA Class OrderJPA
	 */
	@Entity
	@Table(name="practicalcase.order")
	public class OrderJPA implements Serializable, Comparable  {

		private static final long serialVersionUID = 1L;

		private int id;
		private CustomerJPA customer;
		private float totalamount;
		private OrderstatusJPA orderstatus;
		private ShippingAddressJPA shippingaddress;
		private Collection<OrderdetailJPA> orderdetail;
		
		/**
		 * Class constructor methods
		 */
		public OrderJPA() {
			super();
		}
					
		public OrderJPA(int id, CustomerJPA customer, float totalamount,
				OrderstatusJPA orderstatus, ShippingAddressJPA shippingaddress,
				Collection<OrderdetailJPA> orderdetail) {
			super();
			this.id = id;
			this.customer = customer;
			this.totalamount = totalamount;
			this.orderstatus = orderstatus;
			this.shippingaddress = shippingaddress;
			this.orderdetail = orderdetail;
		}

		public OrderJPA(int id, CustomerJPA customer, float totalamount,
				OrderstatusJPA orderstatus, ShippingAddressJPA shippingaddress) {
			super();
			this.id = id;
			this.customer = customer;
			this.totalamount = totalamount;
			this.orderstatus = orderstatus;
			this.shippingaddress = shippingaddress;
		}


		
		/**
		 *  Methods get/set the fields of database
		 */
		@Id
		public int getId() {			
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public float getTotalamount() {
			return totalamount;
		}
		public void setTotalamount(float totalamount) {
			this.totalamount = totalamount;
		}
		
		/**
		 * Methods get/set persistent relationships
		 */
		@OneToOne
		@JoinColumn (name="nif")
		public CustomerJPA getcustomer() {
			return customer;
		}
		public void setCustomer(CustomerJPA customer) {
			this.customer = customer;
		}
		
		@OneToOne
		@JoinColumn (name="idshippingaddress")
		public ShippingAddressJPA getShippingaddress() {
			return shippingaddress;
		}

		public void setShippingaddress(ShippingAddressJPA shippingaddress) {
			this.shippingaddress = shippingaddress;
		}
		@OneToOne
		@JoinColumn (name="orderstatus")
		public OrderstatusJPA getOrderstatus() {
			return orderstatus;
		}
		public void setOrderstatus(OrderstatusJPA orderstatus) {
			this.orderstatus = orderstatus;
		}
		@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
		@JoinColumn(name = "idorder")
		public Collection<OrderdetailJPA> getOrderdetail() {
			if (orderdetail !=null) orderdetail.size();
			return orderdetail;
		}
		public void setOrderdetail(Collection<OrderdetailJPA> orderdetail) {
	 		this.orderdetail = orderdetail; 
		}
		
		public int compareTo(Object o) {
			// TODO Auto-generated method stub
			OrderJPA order=(OrderJPA)o;
			if (this.id>order.getId())
				return 1;
			
			if (this.id<order.getId())
				return -1;
			else
			return 0;
		}


}
